package www.xpu.edu.app.student;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import www.xpu.edu.app.R;

public class Activity_stu_2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_2);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        final ArrayList<String> data = getIntent().getStringArrayListExtra("data");
        Toast.makeText(Activity_stu_2.this,data.toString(),Toast.LENGTH_LONG).show();


        Button button1=findViewById(R.id.title_back);
        Button button2=findViewById(R.id.title_change);
        TextView text_title=findViewById(R.id.title_text);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button2.setVisibility(View.INVISIBLE);
        text_title.setText("投递简历");


        TextView text1=findViewById(R.id.stu2_text1);
        TextView text2=findViewById(R.id.stu2_text2);
        TextView text3=findViewById(R.id.stu2_text3);
        TextView text4=findViewById(R.id.stu2_text4);
        TextView text5=findViewById(R.id.stu2_text5);
        TextView text6=findViewById(R.id.stu2_text6);
        TextView text7=findViewById(R.id.stu2_text7);
        TextView text8=findViewById(R.id.stu2_text8);
        TextView text9=findViewById(R.id.stu2_text9);
        text1.setText("公司名称"+data.get(2));
        text2.setText("招聘类型"+data.get(3));
        text3.setText("工作地点"+data.get(4));
        text4.setText("职位名称"+data.get(5));
        text5.setText("所在部门"+data.get(6));
        text6.setText("工作经验"+data.get(7));
        text7.setText("每月薪水"+data.get(8));
        text8.setText("学历要求"+data.get(9));
        text9.setText("岗位职责及要求\n"+data.get(10));
        Button button=(Button)findViewById(R.id.stu2_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               String num="19829088780";
                int id=Integer.valueOf(data.get(0));
                sendReguest(num,id);
               // Toast.makeText(Activity_stu_2.this,"测试",Toast.LENGTH_LONG).show();
            }
        });


    }
    public void sendReguest(final String num, final int id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                //        http://localhost:8080/xpu/reg1?num=19829088783&password=12345678

                try {
                    //局域网
                    //URL url = new URL("http://192.168.123.196:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    //http://188.131.250.4:8080
                    //http://192.168.123.196:8080/test2/sendresume?num=19829088780&id=9
                    //URL url = new URL("http://188.131.250.4:8080/xpu/reg1" + "?num=" + user + "&password=" + password);
                    //http://192.168.43.188手机热点局域网
                    URL url=new URL("http://188.131.250.4:8080/xpu/sendresume?num="+num+"&id="+id);
                   // URL url=new URL("http://192.168.123.196:8080/test2/sendresume?num=19829088780&id=2");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream ips = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(ips));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                   // Toast.makeText(Activity_stu_2.this,response.toString(),Toast.LENGTH_LONG).show();

                     showResponse(response.toString());

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(Activity_stu_2.this, response, Toast.LENGTH_LONG).show();

            }
        });
    }
}
